﻿#include "Request.h"

Request::Request()
{
	RequestHeader["Host"] = "127.0.0.1";
	RequestHeader["Connection"] = "close";
	RequestHeader["User-Agent"] = "Mozilla/5.0";
	RequestHeader["Accept"] = "*/*";
	RequestHeader["Referer"] = "";
	RequestHeader["Accept-Encoding"] = "deflate";
	RequestHeader["Accept-Language"] = "zh-CN,zh;q=0.9";
	RequestHeader["Cookie"] = "";

	//Accept = "*/*";
	//AcceptEncoding = "deflate";
	/*AcceptLanguage = "zh-CN,zh;q=0.9";
	Connection = "close";
	UserAgent = "Mozilla/5.0";*/

}

void Request::setResPath(const string& path)
{
	resPath = path;
}

void Request::setMethod(const quest q)
{
	if (q == GET)
	{
		method = "GET";
	}
	else if (q == POST)
	{
		method = "POST";
	}
}

void Request::setHost(const string& host)
{
	RequestHeader["Host"] = host;
}

void Request::setHost(const string& host, const int port)
{
	setHost(host);
	setPort(port);
}

void Request::setPort(const int port)
{
	this->port = port;
}

void Request::setReferer(const string& ref)
{
	RequestHeader["Referer"] = ref;
}

void Request::setCookie(const string& cookie)
{
	RequestHeader["Cookie"] = cookie;
}

const string Request::getGetRequest()
{
	string quest;
	quest = method + " " + resPath + getGetFindArg() + "  HTTP/1.1\r\n" +
	RequestHeader.getString("", ": ", "\r\n") + "\r\n";
	return quest;
}

const string Request::getGetFindArg()
{
	string str = "?";
	int n = 0;
	for (auto& data : GetParamStr())
	{
		if (n != 0)
			str += "&";
		str += data.first;
		str += "=";
		str += data.second;
		n++;
	}
	return str;
}

const string Request::getHost()
{
	return RequestHeader["Host"];
}

const int Request::getPort()
{
	return port;
}

const string Request::getResPath()
{
	return resPath;
}
